<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Http\Controllers\home\RegisterController;
use Session;
use Hash;
use DB;

class LoginController extends Controller
{

   /**
   *	前台登陆方法
   */
   //用户名登陆
   public function postDologin(Request $request)
   {
      $username = $_POST['name'];  //用户名
      $password = $_POST['passpwd'];  //密码
      $vcode = $_POST['vcode'];    //验证码

      //用户输入验证码正确
      if(Session::get('vcode') != $vcode)  return 4;
      
      //查询数据库
      $res = DB::table('project_user')->where('username',$username)->first();

      //用户名是否存在
      if(!$res) return 5;

      //判断密码
      if(Hash::check($password,$res->password)) {

         //判断状态
         if($res->status == 0) return 7;

         //查询用户详情表
         $row = DB::table('project_user_details')->where('uid',$res->id)->first();

         // 判断比较是某天的第一次登陆  奖励金币
      
         $arr['gold'] = $row->gold;    //获取金币

         if((date('Ymd',time()) - date('Ymd',$row->lasttime))>=1){

              $arr['gold'] += 10;    //金币加上10
         }

         $arr['lasttime'] = time();   //最后一次登陆时间
         $arr['date'] = date('Y-m-d');
         $arr['time'] = date('H:i:s');
         $arr['lastip'] = $_SERVER["REMOTE_ADDR"];

         //修改详细资料
         $result = DB::table('project_user_details')->where('uid',$res->id)->update($arr);

         $row = DB::table("project_user")->where("id",$res->id)->first();

         if($result) {

            //成功以后将用户的信息存入session中
             session()->set("username",$row);

             return 8;    //跳转首页
         } else {

            return 9;     //登陆失败
         }

      }else {
         return 6;
      }
   }

   //手机号登陆
   public function postDologin1(Request $request)
   {
      $username = $_POST['name'];  //手机号
      $password = $_POST['passpwd'];  //密码
      $vcode = $_POST['vcode'];    //验证码

      //用户输入验证码正确
      if(Session::get('vcode') != $vcode)  return 4;
      
      //查询数据库
      $res = DB::table("project_user")->where("phone",$username)->first();
    
      //用户名是否存在
      if(!$res) return 5;

      //判断密码
      if(Hash::check($password,$res->password)) {

         //判断状态
         if($res->status == 0) return 7;

         //查询用户详情表
         $row = DB::table('project_user_details')->where('uid',$res->id)->first();

         // 判断比较是某天的第一次登陆  奖励金币
      
         $arr['gold'] = $row->gold;    //获取金币

         if((date('Ymd',time()) - date('Ymd',$row->lasttime))>=1){

              $arr['gold'] += 10;    //金币加上10
         }

         $arr['lasttime'] = time();   //最后一次登陆时间
         $arr['date'] = date('Y-m-d');
         $arr['time'] = date('H:i:s');
         $arr['lastip'] = $_SERVER["REMOTE_ADDR"];

         //修改详细资料
         $result = DB::table('project_user_details')->where('uid',$res->id)->update($arr);

         $row = DB::table("project_user")->where("id",$res->id)->first();

         if($result) {

            //成功以后将用户的信息存入session中
             session()->set("username",$row);

             return 8;    //跳转首页
         } else {

            return 9;     //登陆失败
         }

      }else {
         return 6;
      }
   }

    //邮箱登陆
   public function postDologin3(Request $request)
   {
      $username = $_POST['name'];  //手机号
      $password = $_POST['passpwd'];  //密码
      $vcode = $_POST['vcode'];    //验证码

      //用户输入验证码正确
      if(Session::get('vcode') != $vcode)  return 4;
      
      //查询数据库
      $res = DB::table("project_user")->where("email",$username)->first();
    
      //用户名是否存在
      if(!$res) return 5;

      //判断密码
      if(Hash::check($password,$res->password)) {

         //判断状态
         if($res->status == 0) return 7;

         //查询用户详情表
         $row = DB::table('project_user_details')->where('uid',$res->id)->first();

         // 判断比较是某天的第一次登陆  奖励金币
      
         $arr['gold'] = $row->gold;    //获取金币

         if((date('Ymd',time()) - date('Ymd',$row->lasttime))>=1){

              $arr['gold'] += 10;    //金币加上10
         }

         $arr['lasttime'] = time();   //最后一次登陆时间
         $arr['date'] = date('Y-m-d');
         $arr['time'] = date('H:i:s');
         $arr['lastip'] = $_SERVER["REMOTE_ADDR"];

         //修改详细资料
         $result = DB::table('project_user_details')->where('uid',$res->id)->update($arr);

         $row = DB::table("project_user")->where("id",$res->id)->first();

         if($result) {

            //成功以后将用户的信息存入session中
             session()->set("username",$row);

             return 8;    //跳转首页
         } else {

            return 9;     //登陆失败
         }

      }else {
         return 6;
      }
   }

   /**
   *  前台退出方法
   */
   public function getLoginout(Request $request)
   {
    
     $request->session()->forget("username");

     return redirect("/Home");
   }



}
